from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import sys
import os

app = Flask(__name__)

# 创建数据库，并连接flask程序
WIN = sys.platform.startswith('win')
if WIN:
    prefix = 'sqlite:///'
else:
    prefix = 'sqlite:////'
dev_db = prefix + os.path.join(os.path.dirname(app.root_path), 'data.db')
print("dev_db:", dev_db)
app.config['SQLALCHEMY_DATABASE_URI'] = dev_db
app.config['SECRET_KEY'] = 'secret string'
# 设置每次请求结束后会自动提交数据库的改动
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 查询时显示原始SQL语句
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)

# 声明用户表
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)


    def __init__(self, username, email):
        self.username = username
        self.email = email


    # 此方法是实例化对象的自我描述信息，相当于java的toString(),默认情况下返回：类名+object at 内存地址
    def __repr__(self):
        return '<User: %s %s >' % (self.username, self.email)

